package com.gxa.service.impl;

import com.gxa.mapper.AddOrderDataMapper;
import com.gxa.mapper.XsDdMapper;
import com.gxa.pojo.AddOrderData;
import com.gxa.pojo.GoodsStorage;
import com.gxa.service.AddOrderDataService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: TOOD
 * @ClassName: AddOrderDataServiceImpl
 * @Author: ${陈柯宇}
 * Date: 2020/9/25 14:36
 * @Version: 1.0
 */

@Service("addOrderDataService")
public class AddOrderDataServiceImpl implements AddOrderDataService {

    @Resource
    AddOrderDataMapper addOrderDataMapper;

    @Resource
    XsDdMapper xsDdMapper;


    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public Integer insert(Map map) {
        Integer xid = xsDdMapper.selectMaxById();
        map.put("xid",xid);
        return addOrderDataMapper.insert(map);
    }

    @Override
    public Integer insert2(Map map) {
        int i=addOrderDataMapper.insert(map);
        Integer id = (Integer) map.get("id");
        List<Map> selectAodNameByXid = addOrderDataMapper.selectAodNameByXid(id);
        for (Map map1:selectAodNameByXid) {
            String goods = (String) map1.get("productName");
            GoodsStorage selectGoodsByName = addOrderDataMapper.selectGoodsByName(goods);
            if (selectGoodsByName!=null) {
                addOrderDataMapper.updateByGoodsStorage((String)map1.get("lQuantity"),(String)map1.get("sQuantity"),
                        selectGoodsByName.getId());
            }
        }
        return i;
    }


    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Map<String, Object>> selectByAodLike(Map<String, Object> map) {
        return addOrderDataMapper.selectByAodLike(map);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public Integer selectByAodLikeCount(Map map) {
        return addOrderDataMapper.selectByAodLikeCount(map);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Map<String, Object>> selectByUnit() {
        return addOrderDataMapper.selectByUnit();
    }


    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<AddOrderData> findAllByPage(Integer start, Integer pageSize) {
        return addOrderDataMapper.findAllByPage(start,pageSize);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public Integer deleteById(Integer id) {
        return addOrderDataMapper.deleteById(id);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = {Throwable.class})
    public Integer update(Map map) {
        return addOrderDataMapper.update(map);
    }

    @Override
    public List<Map<String, Object>> selectXid(Integer id) {
        return addOrderDataMapper.selectXid(id);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Map<String, Object>> selectAllByShopPage(Integer start, Integer pageSize) {
        return addOrderDataMapper.selectAllByShopPage(start,pageSize);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public List<Map<String, Object>> selectByShopDtLike(Map<String, Object> map) {
        return addOrderDataMapper.selectByShopDtLike(map);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.SUPPORTS,rollbackFor = {Throwable.class})
    public Integer selectByShopDtLikeCount() {
        return addOrderDataMapper.selectByShopDtLikeCount();
    }
}
